package com.cityp.controller;

import com.cityp.bean.ResultBean;
import com.cityp.bean.User;
import com.cityp.service.UserListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userList")
public class UserListController {
    @Autowired
    private UserListService getUserListService;

    /**
     * 用户列表页面
     */
    @RequestMapping(value = "/getalluser",method = RequestMethod.GET)
    @ResponseBody
    public ResultBean getUser(){
        List<User> res = getUserListService.getAll();
        if (res==null) return ResultBean.err("出现错误，请重试");
        else return ResultBean.success("查询成功",res);
    }

    /**
     *  修改资料
     */
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    @ResponseBody
    public ResultBean updateuser(@RequestBody User user){
        int res= getUserListService.updateUser(user);
        if(res==0) return ResultBean.err("修改失败，请重试");
        else return ResultBean.success("修改成功");
    }
    /**
     * 修改用户状态
     */
    @RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
    @ResponseBody
    public ResultBean deleteuser(@RequestBody Map<String,Object> para){
        int res= getUserListService.deleteUser((String)para.get("id"),(int)para.get("state"));
        if(res==0) return ResultBean.err("修改失败，请重试");
        else return ResultBean.success("修改成功");
    }

    /**
     * 修改密码
     */
    @PostMapping(value = "/changePassword")
    public ResultBean changePassword(@RequestBody Map<String,String> para){
        int res=getUserListService.changePassword(para.get("pass"),para.get("id"));
        if(res==0) return ResultBean.err("修改失败，请重试");
        else return ResultBean.success("修改成功");
    }
    /**
     * 获取已冻结或移除的用户
     */
    @GetMapping(value = "/getFreeze")
    public ResultBean getFreeze(){
        List<User> res= getUserListService.getFreeze();
        if(res==null) return ResultBean.err("查询失败，请重试");
        else return ResultBean.success("查询成功",res);
    }

}
